/*
 * Copyright：fxsen <br/>
 * Date: 2022/1/20 16:39 <br/>
 * Email:fxsen@foxmail.com
 * Author: fxsen
 */
package com.fxsen.platform.web.controllers;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.fxsen.commons.core.annotation.RecordLog;
import com.fxsen.commons.core.annotation.RequiredPermission;
import com.fxsen.commons.core.base.result.ResponseResult;
import com.fxsen.commons.core.constant.Constants;
import com.fxsen.commons.sysmanager.core.entity.Permission;
import com.fxsen.commons.sysmanager.core.service.IMenuService;
import com.fxsen.commons.sysmanager.core.service.IPermissionService;
import com.fxsen.commons.sysmanager.core.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
/**
 * @describe: 系统权限控制器
 * @date: 2022/1/20 16:39
 * @author: fxsen
 */
@RestController
@RequestMapping("permission")
@RequiredPermission("permission")
@Api(tags = "系统：权限接口")
public class PermissionController  {
    @Autowired
    private IPermissionService permissionService;
    @Autowired
    private IMenuService menuService;
    @Autowired
    private IUserService userService;
    @ApiOperation(value = "权限列表")
    @GetMapping("list")
    @RequiredPermission("permission:list")
    public ResponseResult list(Permission entity){
        try {
            LambdaQueryWrapper<Permission> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.orderByAsc(Permission :: getOrderIndex);
            List<Permission> list = permissionService.list(queryWrapper);
            return ResponseResult.success(list);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.failed();
        }
    }
    @ApiOperation(value = "保存")
    @PostMapping("save")
    @RecordLog("新增权限")
    @RequiredPermission("permission:save")
    public ResponseResult save(@RequestBody Permission permission, HttpServletRequest request, HttpServletResponse response){
        try {
            if (StringUtils.isBlank(permission.getPid())){
                permission.setPid(Constants.PARENT_ID);
            }
            return permissionService.save(permission) ? ResponseResult.success() : ResponseResult.saveFailed();
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.failed();
        }
    }
    @ApiOperation(value = "更新")
    @PostMapping("update")
    @RecordLog("更新权限")
    @RequiredPermission("permission:update")
    public ResponseResult update(@RequestBody Permission permission, HttpServletRequest request, HttpServletResponse response){
        try {
            if (StringUtils.isBlank(permission.getPid())){
                permission.setPid(Constants.PARENT_ID);
            }
            return permissionService.updateById(permission) ? ResponseResult.success() : ResponseResult.updateFailed();
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.failed();
        }
    }
    @ApiOperation(value = "删除")
    @GetMapping("delete")
    @RecordLog("删除权限")
    @RequiredPermission("permission:delete")
    public ResponseResult delete(@RequestParam("id") String id, HttpServletRequest request, HttpServletResponse response){
        try {
            List<Permission> permissions = permissionService.findByPid(id);
            if (CollectionUtils.isNotEmpty(permissions)){
                return ResponseResult.failed("已有"+ permissions.size()+"个下级，请先清除！");
            }
            return permissionService.removeById(id) ? ResponseResult.success() : ResponseResult.deleteFailed();
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.failed();
        }
    }
    @ApiOperation(value = "状态开关")
    @GetMapping("statuSwitch")
    @RecordLog("修改权限状态")
    @RequiredPermission("permission:statuSwitch")
    public ResponseResult statuSwitch(@RequestParam("id") String id, HttpServletRequest request, HttpServletResponse response){
        try {
            Permission permission = permissionService.getById(id);
            if (permission.getPid().equals(Constants.PARENT_ID)){
                List<Permission> permissions = permissionService.findByPid(permission.getId());
                permissions.forEach(permission1 -> {
                    permission1.setStatus(permission.getStatus().equals(Constants.STATUS_NO) ? Constants.STATUS_YES : Constants.STATUS_NO);
                });
                permissionService.updateBatchById(permissions);
            }
            permission.setStatus(permission.getStatus().equals(Constants.STATUS_NO) ? Constants.STATUS_YES : Constants.STATUS_NO);
            return permissionService.updateById(permission) ? ResponseResult.success() : ResponseResult.updateFailed();
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.failed();
        }
    }
    @ApiOperation(value = "获取树")
    @GetMapping("tree")
    public ResponseResult tree(String permissionIds){
        try {
            List<String> permissionIdList = null;
            if (StringUtils.isNotBlank(permissionIds)){
                permissionIdList = Arrays.asList(permissionIds.split(","));
            }
            List<Permission> permissions = permissionService.selectList();
            /*List<LaYuiTree> list = new LinkedList<>();
            for (Permission permission : permissions) {
                LaYuiTree laYuiTree = new LaYuiTree();
                laYuiTree.setId(permission.getId());
                laYuiTree.setParentId(permission.getPid());
                laYuiTree.setTitle(permission.getName());
                laYuiTree.setChecked(permissionIdList != null && permissionIdList.contains(permission.getId()));
                list.add(laYuiTree);
            }*/
            return ResponseResult.success(permissions);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.failed();
        }
    }
    @ApiOperation(value = "下拉树")
    @GetMapping("selectTree")
    public ResponseResult selectTree(@RequestParam(value = "id",required = false) String id){
        try {
            LambdaQueryWrapper<Permission> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.orderByAsc(Permission :: getOrderIndex);
            List<Permission> list = permissionService.list(queryWrapper);
            return ResponseResult.success(list);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.failed();
        }
    }
}
